<template>
   <div class="zixun-content">
       <Top title="我的收藏"  iconShow='1'></Top>
       <!-- 这个是 顶部的切换栏-->
       <div class="tab">
          <ul>
              <li>
                <a href="./collectProduct.html" >产品</a>  
              </li>
              <li><a href="./collectZiXun.html">资讯</a></li>
              <li>  
                  <a href="javascript:void(0)" class="select">
                        帖子
                      <div class="showborder"></div>
                  </a>  
               </li>
          </ul>
       </div>
       <!-- 这个是编辑 -->
       <div class="zixun-content-edit" @click="goEdit">{{edit?"取消":"编辑"}}</div>
       <!-- 注意这里是加下拉组件的 -->
  <mt-loadmore :top-method="loadTop" ref="loadmore">
        <div class="zixun-content-item" v-for="(item,index) in selectedList" :key="index" > 
            <div class="zixun-content-item-right">
                <div v-if="edit" class="zixun-content-item-right-check">
                    <input type="checkbox" :id="item.visitId" class="chk_1" ref="checkbox" @change="singleSelect" :cId="item.visitId" />
                    <label :for="item.visitId"></label>               
                </div>
                <div @click="goArtical(item.informationId)" class="zixun-content-item-right-content" >
                    <p>{{item.title}}</p>
                    <div class="zixun-content-item-right-content-author clearfix">
                        <div class="zixun-content-item-right-content-log">
                             <img :src="item.head_img" alt="产品">
                         </div>
                         <p>{{item.nick_name}}</p>
                         <p>{{item.create_time | formatDate1}}</p>
                    </div>
                    <!-- <span>{{item.create_time}}</span> -->
                    <div class="zixun-content-item-right-content-icon clearfix">
                       <a href="javascript:void(0)"><img src="../../../assets/comment/pinglun@2x.png" alt=""><span class="red" ref="a">{{item.discuss_count}}</span></a> 
                        <a href="javascript:void(0)"><img src="../../../assets/comment/guanzhu@2x.png" alt=""><span>{{item.collectCount}}</span></a>
                         <a href="javascript:void(0)"><img src="../../../assets/comment/dianzan@2x.png" alt=""><span>{{item.favorCount}}</span></a>
                        <a href="javascript:void(0)">
                            <img src="../../../assets/comment/liulanliang@2x.png" alt="">
                            <span>{{item.readsCount}}</span>
                        </a>   
                    </div>
                </div>        
            </div>
        </div>
  </mt-loadmore>
    <!-- 顶部的固定删除功能 -->
    <div class="deleteDo" v-if="edit">
      <div class="deleteDo-left">
        <label for="checkAll">
          <input type="checkbox" id="checkbox_a1" class="chk_1 checkall" ref="allSelect" @change="allSelect"/>
          <label for="checkbox_a1"></label>
        </label>
        <span style="position:absolute;">全选</span>
        <span>{{selectNum}}/{{selectedList.length}}</span>
      </div>
      <div class="deleteDo-right" @click="shanchu">删除</div>
    </div>
    <!-- 如果一个产品没有，那么空的图片则显示 -->
    <div class="showing" v-if="selectedList.length==0">
          <img src="../../../assets/nothing.png" alt="">
    </div>
    <loading title="加载中..."  v-if="loadMoreShow"></loading>
    <Nomore v-if="noMoreShow"></Nomore>
  </div>
</template>

<script>
import Vue from 'vue'
import '../../../js/lib/flexble.js'
import commons from '../../../common/js/common.js'
import Top from 'components/Header/Header'
import Foot from 'components/Footer/Footer'
import { Indicator, MessageBox,Toast } from "mint-ui";
export default {
  name: 'collectProduct',
  components: { Top,Foot },
  data() {
    return {
           loadMoreShow: false,
            topStatus: '',
            pageNum: 1,
            selectedList: [],
            flag: true,
            noMoreShow: false,
            loadMoreShow: false,
            value: 123,
            arr: [],
            idStr: '',
            edit: false,
            selectNum: 0,
            dedaultImg:{userimg:'../static/img/user.png'} //表示的是用来处理默认图片的
    }
  },
  mounted(){
    Vue.http.interceptors.push((request, next) => { //请求拦截器
        //请求前
        request.headers.set('token', commons.getCookie('token')); //设置请求头
        Indicator.open('加载中...'); //请求loading
        next((response) => {
            //请求后
            Indicator.close(); //隐藏loading
            return response
        })
    })
  },
  created() {
        var _this = this;
        this.$nextTick(function() {
            window.addEventListener('scroll', onScroll)
        })
        // 判断滚动条位置，加载更多
        function onScroll() {
            var scrollTop = document.body.scrollTop;
            var innerHeight = window.innerHeight;
            var offsetHeight = document.body.offsetHeight;
            if (scrollTop + innerHeight >= offsetHeight) {
                if (_this.flag) {
                    if (_this.selectedList.length >= 7) {
                        _this.flag = false;
                        _this.getList()
                    }
                }
            }
        }
    },
    mounted() {
        this.tiezi();
    },
    filters:{  
       formatDate1(val){
           return commons.formatDate1(val);
       }
    },
    methods: {
        handleTopChange: function (status) {  //注意这个是下拉组件中的方法
           this.topStatus = status;
        },
    // 上拉加载数据
    getList() {
        this.pageNum += 1;
        this.loadMoreShow = true;
        var token = commons.getCookie("token");
        var _this = this;
        setTimeout(function() {
            _this.$http.get('/loansupermarket-app/collect/getCustomInformationVisit?visitType=2&token='+commons.getCookie("token")+'&pageNum='+_this.pageNum+'&pageSize=7')
                .then((response) => {
                    console.log(response);
                    var resBody = response.body;
                    if (resBody.code == '000') {
                        _this.flag = true;
                        _this.loadMoreShow = false;
                        if (resBody.data.length < 7) {
                            _this.noMoreShow = true;
                            _this.flag = false;
                        } else {
                            _this.flag = true;
                        }
                        _this.selectedList = _this.selectedList.concat(resBody.data);
                         // 时间戳转成2018-10-08格式
                            _this.selectedList.forEach(function(item, index) {
                                 //item.create_time = dateFormat(item.create_time, 'YYYY-MM-DD HH:mm:ss');
                                if(item.head_img==null||item.head_img==""){ //注意这里用来判断是否后台传进来的为空，然后显示默认的图片
                                item.head_img=_this.dedaultImg.userimg;
                                }
                                //这里用来进行如果后台传入的值是空的就用电话号码代替
                                if(item.nick_name==null||item.head_img==""){
                                    item.nick_name=item.phone;
                                }
                        })
                       
                    } else if(resBody.code == '444') {
                        window.location.href="../user/login.html?history=1";
                    }else{
                       MessageBox.alert(resBody.mesg);
                    }
                })
        }, 500)
    },
    loadTop() {  // 下拉刷新数据的操作
        setTimeout(()=>{
            this.tiezi();
          this.$refs.loadmore.onTopLoaded();
        },500);
    },
    tiezi(){
        this.noMoreShow = false; 
        var token =commons.getCookie("token");
        // 加载第一页数据并实例化
        let _this=this;
          this.$http.get('/loansupermarket-app/collect/getCustomInformationVisit?visitType=2&token='+commons.getCookie("token")+'&pageNum='+1+'&pageSize=7')
          .then((response) => {
                var resBody = response.body;
                if (resBody.code == '000') {
                    this.selectedList.splice(0,resBody.data.length); //下拉请求新的数据，并且将第一页的数据删掉
                  this.selectedList = resBody.data.concat(this.selectedList); //表示的是将最新的数据插在最前面
                    if (this.selectedList.length < 7) {
                        this.flag = false;
                        this.noMoreShow = true; 
                    }else {
                            this.flag = true;
                        }
                          // 时间戳转成2018-10-08格式
                        _this.selectedList.forEach(function(item, index) {
                            // item.create_time = dateFormat(item.create_time, 'YYYY-MM-DD HH:mm:ss');
                            if(item.head_img==null||item.head_img==""){ //注意这里用来判断是否后台传进来的为空，然后显示默认的图片
                            item.head_img=_this.dedaultImg.userimg;
                            }
                            //这里用来进行如果后台传入的值是空的就用电话号码代替
                            if(item.nick_name==null||item.head_img==""){
                                item.nick_name=item.phone;
                            }
                        })
                }else if(resBody.code == '444'){
                     window.location.href="../user/login.html?history=1";
                }else{
                    MessageBox.alert(resBody.mesg);
                }
            })
        },
        // 去编辑
        goEdit() {
            this.edit = !this.edit;
            if (!this.edit) {
                this.selectNum = 0;
            }
        },
        // 单选
        singleSelect() {
            var _this = this;
            var flag = true;
            this.$refs.checkbox.forEach(function(item, index) {
                if (!item.checked) {
                    _this.$refs.allSelect.checked = false;
                    flag = false;
                    return false;
                }
                if (flag) {
                    _this.$refs.allSelect.checked = true;
                }
            })
            // 判断已选列表个数
            this.judgeNum();
        },
        // 全选
        allSelect() {
            if (this.$refs.allSelect.checked) {
                this.$refs.checkbox.forEach(function(item, index) {
                    item.checked = true;
                })
            } else {
                this.$refs.checkbox.forEach(function(item, index) {
                    item.checked = false;
                })
            }
            this.judgeNum()
        },
        // 判断已选列表个数
        judgeNum() {
            var _this = this;
            _this.selectNum = 0;
            this.$refs.checkbox.forEach(function(item, index) {
                if (item.checked) {
                    _this.selectNum += 1
                }
            })
        },
        // 删除
       shanchu() {
          var _this = this;
          var token =commons.getCookie("token");
            // 添加了一个属性cId，根据属性获取产品id再添加到数组提交到后台
            this.$refs.checkbox.forEach(function(item, index) {
                if (item.checked) {
                _this.arr.push(item.getAttribute('cId'));
                _this.idStr = _this.arr.join(',');
                }
            })
            if (this.idStr.length == 0) {
                Toast('请勾选你要删除的帖子~');
            } else {
                this.$http.post('/loansupermarket-app/collect/deleteInformationVisit', { visitType: 2, idStr: this.idStr, token: token }).then(function(response) {
                if (response.body.code == '000') {
                    Toast('删除成功！');
                    setTimeout(function() {
                    window.location.reload()
                    }, 500)
                } else if (response.body.code == '444') {
                     window.location.href="../user/login.html?history=1";
                } else {
                    MessageBox.alert(response.body.mesg);
                }
                })
           }
        },
        goArtical(id) {
          window.location.href="../bbs/article.html?id="+id;
        }    
    }
}
</script>
<style lang="scss">
@import '../../../scss/base.scss';
.lo .weui-loadmore{
  margin:0px auto;
  line-height:0;
}
.lo{
  padding:1.2em 0 0 0;
}
//这个是tab栏的样式
.tab{
     border-bottom: 1px solid #e4e4e4;
      margin-top: 1.2rem;
      position: fixed!important;
      width: 100%;
      top: 0;
      background: #fff;
      z-index: 3;
      height: 1.2rem;
      line-height: 1.2rem;
      display: flex;
    >ul{
      display:flex;
       width: 100%;
      height: 100%;
      box-sizing: border-box;
      li{   
        font-size:14px;
        
        font-weight: bold;
        position:relative; 
          flex:1;
          text-align:center;
          &:after{
            content:'';
            position:absolute;
            width:1px;
            height:rem(50);
            background: #e4e4e4;
            right:0px;
            top:50%;
            margin-top:rem(-25);
          }
          a{
            display: block;
            color: #666;
          }
           .select{
                 color: rgb(48, 204, 219);
             }
      }
      .showborder{
        position:absolute;
        width:rem(150);
        height:1px;
        background:rgb(48, 204, 219);
        bottom:-1px;
        left:50%;
        margin-left:rem(-75);
      }
      
    }
}
.showing{
  margin-top: rem(120);
}
.zixun-content {
        text-align: center;
        margin-top: 2.7rem;
    // width: 100%;
    box-sizing: border-box;
    &-edit {
        position: fixed;
        right: 0.4rem;
        top: 0.35rem;
        z-index: 999;
        color: #fff;
        font-size: 0.4rem;
    }
    .weui-loadmore_line {
        margin-top: 0.6rem;
    } 
    &-item {
        margin-bottom: rem(20);
        padding: 0.4rem 0.4rem rem(30) 0.4rem;
        background: #fff;
        box-sizing: border-box;
        width: 100%;
        &-right {
            margin-left: 0.27rem;
            text-align: left;
            display: flex;
            &-check {
                flex:1;
                input {
                    margin-top: 0.45rem;
                    width: 0.4rem;
                    height: 0.5rem;
                    border-radius: 50%;
                }
            }
            &-content {
                flex:9;    
                p {
                    font-size:rem(32); // height: 0.7rem;
                    line-height: 0.7rem;
                    color: #333333;
                    overflow: hidden;
                    
                }
                &-author{
                    
                     margin-top:rem(20);
                     p{
                        line-height:1rem;
                        float: left;
                     }
                     p:nth-of-type(1){
                         font-size:rem(26);
                         color: #666666;
                         margin:0 rem(20) 0 rem(10);
                        
                     }
                      p:nth-of-type(2){
                         font-size:rem(24);
                         color: #999999;
                         line-height:1rem;
                     }
                }
                &-log{            
                    float: left;
                    width: 1rem;
                    height: 1rem;
                    border-radius: 50%;
                    margin:0rem auto;
                    overflow: hidden;
                    img{
                        width: 100%;
                        height: 100%;
                    }
                } 
                &-icon{
                    
                    a{
                        margin-right:5px;
                        float:right;
                         display:block;
                        img{
                            width:rem(35);
                            height:rem(35);
                            vertical-align: middle;
                            padding-right:2px;
                           padding-bottom: 2px;
                        }
                        span{
                            padding:0 3px;
                            font-size: rem(24);
                            color: #999999;
                        }     
                    }
                    .red{
                        color: #ff5b52;
                    }
                }
            }
        }
    }
    .deleteDo {
         z-index:1000;
        display: flex;
        width: 100%;
        position: fixed;
        left: 0;
        height: 1.3rem; // line-height: 1.3rem;
        color: #fff;
        bottom: 0;
        &-left {
            flex: 6;
            background: rgba(0, 0, 0, 0.4);
            border-right: 1px solid #fff;
            text-align: left;
            box-sizing: border-box;
            padding: 0 0.4rem; // input {
            span {
                float: right;
                line-height: 1.3rem;
            }
        }
        &-right {
            font-size: 0.3rem;
            flex: 3;
            background: rgba(0, 0, 0, 0.4);
            line-height: 1.3rem;
        }
    }
    .chk_1 {

        display: none; // vertical-align: -15%;
        width: 0.3rem;
        height: 0.3rem;
    }
    .chk_1+label {
        margin-top: 0.35rem;
        color:#fff !important;
        background-color: #FFF;
        border: 1px solid #C1CACA;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05);
        padding: 0.2rem;
        border-radius: 50%;
        display: inline-block;
        position: relative;
        margin-right: 30px; // display: none;
        // vertical-align: -15%;
        width: 0.1rem;
        height: 0.1rem;
    }
    .chk_1+label:active {

        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px 1px 3px rgba(0, 0, 0, 0.1);
    }
    .chk_1:checked+label {
        margin-top: 0.35rem;
        background-color: $primaryColor;
        border: 1px solid $primaryColor;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), inset 0px -15px 10px -12px rgba(0, 0, 0, 0.05), inset 15px 10px -12px rgba(255, 255, 255, 0.1);
        color: #243441;
    }
    .chk_1:checked+label:after {
        content: '\2714'; //勾选符号 
        position: absolute;
        top: 0px;
        left: 0px;
        color: #fff !important;
        width: 100%;
        text-align: center;
        font-size: 0.2rem; // padding: 1px 0 0 0;
        vertical-align: text-top;
    }
}
</style>
